/*
 *说明：选择时间策略脚本
 *
 *@author liuxun 2012-03-19
*/

var opeForm;
var grid;

$(function(){
	opeForm = $('#opeForm');

	grid=$('#listTable').datagrid({
		title: '时间策略列表',
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[
		    
		    {title:'时间策略名称',field:'time_policy_name',align:'center',width:80},

		    {title:'开始日期',field:'start_date',align:'center',width:80},

		    {title:'结束日期',field:'end_date',align:'center',width:80},
		            
		    {title:'开始时间',field:'start_time',align:'center',width:80},
		         
		    {title:'结束时间',field:'end_time',align:'center',width:80},

		    {title:'星期',field:'week',align:'center',width:120,
            	formatter:function(value, res){
            		var str = '';  
            		var arr = value.split("");//全部分割   
            		for (i = 0; i < arr.length; i++) {
            			var j = i;
            			   if(arr.length==j+1){
            				   str += arr[i];
            			   }else{
            				   str += arr[i] +'、' ;
            			   }
            		}
            		return str;
            	}		    	
		    },

		    {title:'时间策略描述',field:'time_policy_desc',align:'center',width:110}
        ]],
		pagination:true,
		singleSelect: true,
		onDblClickRow: function(rowIndex, rowData) {
	    	window.returnValue = rowData;
	    	window.close();
	    }
	});

	queryOperation();
	
	$('#addBtn').click(addTimePolicyOperation);
	$('#deleteBtn').click(deleteTimePolicyOperation);
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'D_TIME_POLICY!queryList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

function addTimePolicyOperation(){
	
	var rows = grid.datagrid('getRows');
	
	if(!isEmpty(rows)){
		showInfoWin('用户已经授权时间策略！');
		return;
	}
	
	var action = "D_TIME_POLICY!showTimePolicySelect.do";
	var row = showModalWin(action, 'A2');
	
	if (row) {
		var usr_id = $('#usr_id').val();
		var time_policy_id = row.time_policy_id;
		
		var params = {'model.usr_id': usr_id, 'model.time_policy_id': time_policy_id};
		$ .ajax({
			url: 'A_USR!doUpdateA_USRbyTimePolicy.do',
			data: params,
			type: 'post',
			success: function(data){
			    showInfoWin('操作成功!');
				afterOK();
			}
		});
	}	
}

//删除时间策略
function deleteTimePolicyOperation() {
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要删除的记录!');	
		return;
	}
	var usr_id = $('#usr_id').val();
	var params = $ .param({'model.usr_id' : usr_id}, true);

	$ .messager.confirm('提示信息', '您确定要删除选定的时间策略吗?', function(result){
		if(result){
			$ .ajax({
				url: 'A_USR!deleteTimePolicyOperation.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}